package cn.fzkj.gulimall.controller;

import cn.fzkj.common.exception.ErrorsEnum;
import cn.fzkj.common.to.es.SkuEsModel;
import cn.fzkj.common.utils.R;
import cn.fzkj.gulimall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @DESCRIPTION es
 * @Author yaya
 * @DATE 2022/5/14
 */
@Slf4j
@RequestMapping(value = "/search")
@RestController
public class ElasticController {

    @Autowired
    private ProductService productService;

    /** 上架商品 */
    @PostMapping(value = "/save/product")
    public R productsUp(@RequestBody List<SkuEsModel> skuEsModelList){

        boolean success = false;
        try {
            success = productService.productsUp(skuEsModelList);
        } catch (IOException e) {
            log.warn("elasticsearch 商品上架异常 -> {}", e);
            return R.error(ErrorsEnum.PRODUCT_UP_EXCEPTION);
        }
        if (success){
            return R.ok();
        }
        return R.error();
    }
}
